package com.xiaobug.demo.oss;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;

import com.alibaba.sdk.android.oss.ClientException;
import com.alibaba.sdk.android.oss.ServiceException;
import com.alibaba.sdk.android.oss.model.PutObjectRequest;
import com.alibaba.sdk.android.oss.model.PutObjectResult;
import com.ljb.commlibrary.oss.OssUtils;
import com.ljb.commlibrary.oss.ProgressCallback;
import com.ljb.commlibrary.oss.v20.OssUtilsV20;
import com.ljb.commlibrary.oss.v20.PutObjectCallbackV20;
import com.ljb.commlibrary.oss.v20.UploadConfig;
import com.ljb.commlibrary.utils.L;
import com.xiaobug.baselibrary.base.BaseActivity;
import com.xiaobug.demo.R;
import com.xiaobug.demo.databinding.OssActivityTestBinding;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by ljb on 2018/5/4.
 */

public class OssTestActivity extends BaseActivity<OssActivityTestBinding> {
    public class Presenter {
        public void onClick1(View view) {
            L.e("点击了");
            testUploadV20();
        }

        public void onClick2(View view) {
            testUploadListV20();
        }

        public void onClick3(View view) {
            testUpload();
        }
    }

    String path = Environment.getExternalStorageDirectory().toString() + File.separator + "ceshi.png";
    String path2 = Environment.getExternalStorageDirectory().toString() + File.separator + "ceshi2.png";

    public static void actionStart(Context context) {
        context.startActivity(new Intent(context, OssTestActivity.class));
    }

    @Override
    protected int getRootID() {
        return R.layout.oss_activity_test;
    }

    @Override
    protected void initData(Bundle savedInstanceState, OssActivityTestBinding binding) {
        binding.setP(new Presenter());
    }

    /**
     * 测试上传
     */
    private void testUpload() {
        OssUtils.getInstance().uploadObject(OssConfig.bucketName_, "objectKey", path2, new ProgressCallback<PutObjectRequest, PutObjectResult>() {
            @Override
            public void onProgress(PutObjectRequest putObjectRequest, long currentSize, long totalSize) {
                Log.e(TAG, "onProgress: " + currentSize);
            }

            @Override
            public void onSuccess(PutObjectRequest putObjectRequest, PutObjectResult putObjectResult) {
                Log.e(TAG, "onSuccess: ");
            }

            @Override
            public void onFailure(PutObjectRequest putObjectRequest, ClientException clientException, ServiceException serviceException) {
                Log.e(TAG, "onFailure: ");
            }
        });
    }

    /**
     * 测试上传v2.0
     */
    private void testUploadV20() {
        UploadConfig config = new UploadConfig("z_test/key00.png", path2);
        OssUtilsV20.getInstance().uploadObject(OssConfig.bucketName_, config, new PutObjectCallbackV20() {
            @Override
            public void onSuccess(UploadConfig uploadConfig) {
                Log.e(TAG, "onSucess: " + uploadConfig.toString());
            }

            @Override
            public void onFailure(UploadConfig uploadConfig, String msg) {
                Log.e(TAG, "onFailure: " + msg);
            }

            @Override
            public void onProgress(UploadConfig uploadConfig, long currentSize, long totalSize) {
                Log.e(TAG, "onProgress: " + uploadConfig.objectKey + "currentSize=" + currentSize + ",totalSize=" + totalSize);
            }
        });
//        OssUtilsV20.getInstance().cancel(config.task);

    }

    /**
     * 测试多文件上传v2.0
     */
    private void testUploadListV20() {
        List<UploadConfig> configs = new ArrayList<>();
        for (int i = 0; i < 7; i++) {
            configs.add(new UploadConfig("z_test/key" + i + ".png", path));
        }
        OssUtilsV20.getInstance().uploadObjectList(OssConfig.bucketName_, configs, new PutObjectCallbackV20() {
            @Override
            public void onSuccess(UploadConfig uploadConfig) {
                Log.e(TAG, "onSucess: " + uploadConfig.toString());
            }

            @Override
            public void onFailure(UploadConfig uploadConfig, String msg) {
                Log.e(TAG, "onFailure: " + msg);
            }

            @Override
            public void onProgress(UploadConfig uploadConfig, long currentSize, long totalSize) {
                Log.e(TAG, "onProgress: " + uploadConfig.objectKey + "currentSize=" + currentSize + ",totalSize=" + totalSize);
            }
        });
//        for (UploadConfig config : configs) {
//            OssUtilsV20.getInstance().cancel(config.task);
//        }
    }
}
